package com.lancoo.answer.util;

import android.text.TextUtils;
import android.util.Patterns;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lancoo.answer.model.bean.TextInfo;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Create by TangJun on 2019/2/16
 */
public class FormatUtil {

    /**
     * @param floatV
     * @return 判断浮点数是否是整数
     */
    public static Boolean IsInt(float floatV) {
        float f = floatV;
        int i = (int) f;
        return i == f;
    }

    public static String getShowStr_KeepTwoDecimalPlaces(float floatV) {
        if (IsInt(floatV)) {
            return (int) floatV + "'";
        }
        return getFloat_KeepTwoDecimalPlaces(floatV) + "";
    }

    public static String getShowStr_KeepOneDecimalPlaces(float floatV) {
        if (IsInt(floatV)) {
            return (int) floatV + "'";
        }
        return getFloat_KeepOneDecimalPlaces(floatV) + "";
    }

    /**
     * @param floatV 保留两位小数点，舍入为四舍五入
     * @return
     */
    public static float getFloat_KeepTwoDecimalPlaces(float floatV) {
        String value = getFloatFormat_KeepTwoDecimalPlaces().format(floatV);
        float a = Float.valueOf(value);
        return a;
    }

    /**
     * @param doubleV 保留两位小数点，舍入为四舍五入
     * @return
     */
    public static float getFloat_KeepTwoDecimalPlaces(Double doubleV) {
        String value = getFloatFormat_KeepTwoDecimalPlaces().format(doubleV);
        float a = Float.valueOf(value);
        return a;
    }

    /**
     * @param floatV 保留1位小数点，舍入为四舍五入
     * @return
     */
    public static float getFloat_KeepOneDecimalPlaces(float floatV) {
        String value = getFloatFormat_KeepOneDecimalPlaces().format(floatV);
        float a = Float.valueOf(value);
        return a;
    }

    /**
     * @param doubleV 保留1位小数点，舍入为四舍五入
     * @return
     */
    public static float getFloat_KeepOneDecimalPlaces(Double doubleV) {
        String value = getFloatFormat_KeepOneDecimalPlaces().format(doubleV);
        float a = Float.valueOf(value);
        return a;
    }

    /**
     * @param floatV
     * @return 舍入为四舍五入
     */
    public static int getInt(float floatV) {
        String value = getIntFormat().format(floatV);
        int a = Integer.valueOf(value);
        return a;
    }


    /**
     * @param doubleV
     * @return 舍入为四舍五入
     */
    public static int getInt(Double doubleV) {
        String value = getIntFormat().format(doubleV);
        int a = Integer.valueOf(value);
        return a;
    }

    private static DecimalFormat getFloatFormat_KeepTwoDecimalPlaces() {
        DecimalFormat df = new DecimalFormat("0.0#");
        df.setRoundingMode(RoundingMode.HALF_UP);
        return df;
    }

    private static DecimalFormat getFloatFormat_KeepOneDecimalPlaces() {
        DecimalFormat df = new DecimalFormat("0.#");
        df.setRoundingMode(RoundingMode.HALF_UP);
        return df;
    }

    private static DecimalFormat getIntFormat() {
        DecimalFormat df = new DecimalFormat("0");
        df.setRoundingMode(RoundingMode.HALF_UP);
        return df;
    }

    public static boolean isSpecialChar(String str) {
        String regEx = "[ _`~!@#$%∧&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]|\n|\r|\t";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }

    public static boolean isContainSpecialChar(String str) {
        String regEx = "[`!:;,.?~！……——|；：。，、？]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }

    public static Boolean isRightIp(String ip) {
        if (TextUtils.isEmpty(ip)) return false;

        String regex = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";

        // 匹配1 和匹配2均可实现Ip判断的效果
        Pattern pattern = Pattern.compile(regex);
        try {
            return pattern.matcher(ip).matches();
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean isPortLegal(int port) {
        return port >= 1 && port <= 65535;
    }

    public static Boolean isUrlLegalL(String url) {
        return !TextUtils.isEmpty(url) && Patterns.WEB_URL.matcher(url).matches();
    }

    public static String gson(Object o) {
        Gson g = new GsonBuilder().serializeNulls().create();
        return g.toJson(o);
    }

    /**
     * 时间格式类
     *
     * @param day
     * @param month
     * @return
     */
    public static String getDateStr(int day, int month, int hour, int minute) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm");
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"));
        if (day != 0) {
            calendar.add(Calendar.DAY_OF_MONTH, day);
        }
        if (month != 0) {
            calendar.add(Calendar.MONTH, month);
        }
        calendar.add(Calendar.HOUR_OF_DAY, hour);
        calendar.add(Calendar.MINUTE, minute);
        String dayStr = sdf.format(calendar.getTime());
        return dayStr;
    }

    public static Long getDateLong(int day, int month, int year, int hour, int minute) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm");
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"));
        if (day != 0) {
            calendar.add(Calendar.DAY_OF_MONTH, day);
        }
        if (month != 0) {
            calendar.add(Calendar.MONTH, month);
        }
        if (year != 0) {
            calendar.add(Calendar.YEAR, year);
        }
        calendar.add(Calendar.HOUR_OF_DAY, hour);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTimeInMillis() / 1000;
    }

    /**
     * 数字化答案转成文字
     *
     * @param position
     * @param textInfoBeans
     * @return
     */
    public static String toStr(int position, List<TextInfo> textInfoBeans) {
        TextInfo answerBean = textInfoBeans.get(position);
        int type = answerBean.getStuType();
        String before;
        String center;
        String after;
        if (position == 0) {
            if (isContainSpecialChar(textInfoBeans.get(1).getText())) {
                center = "(" + textInfoBeans.get(2).getText() + ")";
                after = "(" + textInfoBeans.get(3).getText() + ")";
                if (type == 2) {
                    before = "∧(" + textInfoBeans.get(0).getText() + ")";
                    return before + center + "→" + answerBean.getStuAnswer();
                } else if (type == 3) {
                    before = "(" + textInfoBeans.get(0).getText() + ")∧";
                    return before + center + "→" + answerBean.getStuAnswer();
                } else {
                    before = textInfoBeans.get(0).getText();
                    if (type == 0) {
                        return before + " " + center + " " + after + "→删除" + answerBean.getText();
                    } else {
                        return before + " " + center + " " + after + "→" + answerBean.getStuAnswer();
                    }
                }
            } else if (isContainSpecialChar(textInfoBeans.get(position + 2).getText())) {
                center = "(" + textInfoBeans.get(position + 1).getText() + ")";
                after = "(" + textInfoBeans.get(position + 3).getText() + ")";
                if (type == 2) {
                    before = "∧(" + textInfoBeans.get(0).getText() + ")";
                    return before + center + "→" + answerBean.getStuAnswer();
                } else if (type == 3) {
                    before = "(" + textInfoBeans.get(0).getText() + ")∧";
                    return before + center + "→" + answerBean.getStuAnswer();
                } else {
                    before = textInfoBeans.get(0).getText();
                    if (type == 0) {
                        return before + " " + center + " " + after + "→删除" + answerBean.getText();
                    } else {
                        return before + " " + center + " " + after + "→" + answerBean.getStuAnswer();
                    }
                }
            } else {
                center = "(" + textInfoBeans.get(1).getText() + ")";
                after = "(" + textInfoBeans.get(2).getText() + ")";
                if (type == 2) {
                    before = "∧(" + textInfoBeans.get(0).getText() + ")";
                    return before + center + "→" + answerBean.getStuAnswer();
                } else if (type == 3) {
                    before = "(" + textInfoBeans.get(0).getText() + ")∧";
                    return before + center + "→" + answerBean.getStuAnswer();
                } else {
                    before = textInfoBeans.get(0).getText();
                    if (type == 0) {
                        return before + " " + center + " " + after + "→删除" + answerBean.getText();
                    } else {
                        return before + " " + center + " " + after + "→" + answerBean.getStuAnswer();
                    }
                }
            }
        } else if (position == textInfoBeans.size() - 2) {
            if (isContainSpecialChar(textInfoBeans.get(position - 1).getText())) {
                before = "(" + textInfoBeans.get(position - 3).getText() + ")";
                center = "(" + textInfoBeans.get(position - 2).getText() + ")";
                if (type == 2) {
                    after = "∧(" + textInfoBeans.get(position).getText() + ")";
                    return center + after + "→" + answerBean.getStuAnswer();
                } else if (type == 3) {
                    after = "(" + textInfoBeans.get(position).getText() + ")∧";
                    return center + after + "→" + answerBean.getStuAnswer();
                } else {
                    after = textInfoBeans.get(position).getText();
                    if (type == 0) {
                        return before + " " + center + " " + after + "→删除" + answerBean.getText();
                    } else {
                        return before + " " + center + " " + after + "→" + answerBean.getStuAnswer();
                    }
                }
            } else if (isContainSpecialChar(textInfoBeans.get(position - 2).getText())) {
                before = "(" + textInfoBeans.get(position - 3).getText() + ")";
                center = "(" + textInfoBeans.get(position - 1).getText() + ")";
                if (type == 2) {
                    after = "∧(" + textInfoBeans.get(position).getText() + ")";
                    return center + after + "→" + answerBean.getStuAnswer();
                } else if (type == 3) {
                    after = "(" + textInfoBeans.get(position).getText() + ")∧";
                    return center + after + "→" + answerBean.getStuAnswer();
                } else {
                    after = textInfoBeans.get(position).getText();
                    if (type == 0) {
                        return before + " " + center + " " + after + "→删除" + answerBean.getText();
                    } else {
                        return before + " " + center + " " + after + "→" + answerBean.getStuAnswer();
                    }
                }
            } else {
                before = "(" + textInfoBeans.get(position - 2).getText() + ")";
                center = "(" + textInfoBeans.get(position - 1).getText() + ")";
                if (type == 2) {
                    after = "∧(" + textInfoBeans.get(position).getText() + ")";
                    return center + after + "→" + answerBean.getStuAnswer();
                } else if (type == 3) {
                    after = "(" + textInfoBeans.get(position).getText() + ")∧";
                    return center + after + "→" + answerBean.getAnswer();
                } else {
                    after = textInfoBeans.get(position).getText();
                    if (type == 0) {
                        return before + " " + center + " " + after + "→删除" + answerBean.getText();
                    } else {
                        return before + " " + center + " " + after + "→" + answerBean.getStuAnswer();
                    }
                }
            }
        } else {
            if (isContainSpecialChar(textInfoBeans.get(position - 1).getText()) && isContainSpecialChar(textInfoBeans.get(position + 1).getText())) {
                before = "(" + textInfoBeans.get(position - 2).getText() + ")";
                after = "(" + textInfoBeans.get(position + 2).getText() + ")";
                if (type == 2) {
                    center = "∧(" + textInfoBeans.get(position).getText() + ")";
                    return before + center + "→" + answerBean.getStuAnswer();
                } else if (type == 3) {
                    center = "(" + textInfoBeans.get(position).getText() + ")∧";
                    return center + after + "→" + answerBean.getStuAnswer();
                } else {
                    center = textInfoBeans.get(position).getText();
                    if (type == 0) {
                        return before + " " + center + " " + after + "→删除" + answerBean.getText();
                    } else {
                        return before + " " + center + " " + after + "→" + answerBean.getStuAnswer();
                    }
                }
            } else if (isContainSpecialChar(textInfoBeans.get(position - 1).getText()) && !isContainSpecialChar(textInfoBeans.get(position + 1).getText())) {
                before = "(" + textInfoBeans.get(position - 2).getText() + ")";
                after = "(" + textInfoBeans.get(position + 1).getText() + ")";
                if (type == 2) {
                    center = "∧(" + textInfoBeans.get(position).getText() + ")";
                    return before + center + "→" + answerBean.getStuAnswer();
                } else if (type == 3) {
                    center = "(" + textInfoBeans.get(position).getText() + ")∧";
                    return center + after + "→" + answerBean.getStuAnswer();
                } else {
                    center = textInfoBeans.get(position).getText();
                    if (type == 0) {
                        return before + " " + center + " " + after + "→删除" + answerBean.getText();
                    } else {
                        return before + " " + center + " " + after + "→" + answerBean.getStuAnswer();
                    }
                }
            } else if (!isContainSpecialChar(textInfoBeans.get(position - 1).getText()) && isContainSpecialChar(textInfoBeans.get(position + 1).getText())) {
                before = "(" + textInfoBeans.get(position - 1).getText() + ")";
                after = "(" + textInfoBeans.get(position + 2).getText() + ")";
                if (type == 2) {
                    center = "∧(" + textInfoBeans.get(position).getText() + ")";
                    return before + center + "→" + answerBean.getStuAnswer();
                } else if (type == 3) {
                    center = "(" + textInfoBeans.get(position).getText() + ")∧";
                    return center + after + "→" + answerBean.getStuAnswer();
                } else {
                    center = textInfoBeans.get(position).getText();
                    if (type == 0) {
                        return before + " " + center + " " + after + "→删除" + answerBean.getText();
                    } else {
                        return before + " " + center + " " + after + "→" + answerBean.getStuAnswer();
                    }
                }
            } else {
                before = "(" + textInfoBeans.get(position - 1).getText() + ")";
                after = "(" + textInfoBeans.get(position + 1).getText() + ")";
                if (type == 2) {
                    center = "∧(" + textInfoBeans.get(position).getText() + ")";
                    return before + center + "→" + answerBean.getStuAnswer();
                } else if (type == 3) {
                    center = "(" + textInfoBeans.get(position).getText() + ")∧";
                    return center + after + "→" + answerBean.getStuAnswer();
                } else {
                    center = textInfoBeans.get(position).getText();
                    if (type == 0) {
                        return before + " " + center + " " + after + "→删除" + answerBean.getText();
                    } else {
                        return before + " " + center + " " + after + "→" + answerBean.getStuAnswer();
                    }
                }
            }
        }
    }


    /**
     * 拼接答案
     *
     * @param answerBean
     * @param answerStr
     * @return
     */
    public static String toArr(TextInfo answerBean, String answerStr) {
        String numStr;
        if (answerBean.getType() == 0) {
            numStr = answerBean.getIndex() + "*|" + answerBean.getStuType() + "*|";
        } else {
            numStr = answerBean.getIndex() + "*|" + answerBean.getStuType() + "*|" + answerBean.getStuAnswer();
        }
        return numStr + "@|" + answerStr;
    }

    /**
     * 转成bean
     *
     * @param str
     * @return
     */
    public static int getPosition(String str) {
        String[] strs = str.split("\\*\\|");
        return Integer.parseInt(strs[0]);
    }


    /**
     * 解析多个答案
     *
     * @param str
     * @return
     */
    public static List<String> toList(String str, String regex) {
        String[] strs = str.split(regex);
        return new ArrayList<>(Arrays.asList(strs));
    }

    /**
     * 提供（相对）精确的除法运算。当发生除不尽的情况时，由scale参数指
     * 定精度，以后的数字四舍五入。
     *
     * @param v1    被除数
     * @param v2    除数
     * @param scale 表示表示需要精确到小数点以后几位。
     * @return 两个参数的商
     */
    public static BigDecimal div(double v1, double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP);
    }
}

